package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jf3q.common.domain.EduChapter;
import com.jf3q.common.domain.EduCourse;
import com.jf3q.common.domain.EduCourseDescription;
import com.jf3q.common.domain.EduSubject;
import com.jf3q.common.domain.EduTeacher;
import com.jf3q.common.mapper.EduCourseDescriptionMapper;
import com.jf3q.common.mapper.EduCourseMapper;
import com.jf3q.common.mapper.EduSubjectMapper;
import com.jf3q.common.mapper.EduTeacherMapper;
import com.jf3q.common.service.ChapterService;
import com.jf3q.common.service.VideoService;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.front.vo.CourseWebVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    EduCourseMapper courseMapper;
    @Autowired
    EduTeacherMapper teacherMapper;
    @Autowired
    EduSubjectMapper subjectMapper;
    @Autowired
    EduCourseDescriptionMapper courseDescriptionMapper;
    @Autowired
    ChapterService chapterService;
    @Autowired
    VideoService videoService;

    @GetMapping("/getFrontCourseInfo/{id}")
    public ResultVo getFrontCourseInfo(@PathVariable("id") String id) {
        EduCourse eduCourse = courseMapper.selectById(id);
        CourseWebVo courseWebVo=new CourseWebVo();
        BeanUtils.copyProperties(eduCourse,courseWebVo);
        EduSubject eduSubject = subjectMapper.selectById(eduCourse.getSubjectId());
        EduSubject eduSubject1 = subjectMapper.selectOne(new LambdaQueryWrapper<EduSubject>().eq(EduSubject::getId,eduCourse.getSubjectParentId()));
        courseWebVo.setSubjectLevelTwo(eduSubject1.getTitle());
        courseWebVo.setSubjectLevelOne(eduSubject.getTitle());
        EduTeacher eduTeacher = teacherMapper.selectById(eduCourse.getTeacherId());
        courseWebVo.setTeacherName(eduTeacher.getName());
        courseWebVo.setTeacherAvatar(eduTeacher.getAvatar());
        courseWebVo.setTeacherIntro(eduTeacher.getIntro());
        EduCourseDescription courseDescription = courseDescriptionMapper.selectOne(new LambdaQueryWrapper<EduCourseDescription>().eq(EduCourseDescription::getId, eduCourse.getId()));
        if (courseDescription!= null){
            courseWebVo.setDescription(courseDescription.getDescription());
        }
        List<EduChapter> chapters = chapterService.list()
                .stream()
                .filter(d -> d.getCourseId().equals(id))
                .toList();
        List<EduChapter> chapters1 = chapters.stream()
                .peek(p -> p.setChildren(videoService.list().stream().filter(f -> f.getChapterId().equals(p.getId())).toList()))
                .toList();


        Map map=new HashMap();
        map.put("courseWebVo",courseWebVo);
        map.put("chapterVideoList",chapters1);
        return ResultVo.success("success", map);
    }
}
